//----------------------------------------------------//
//----------------- GLOBAL FUNCTIONS -----------------//
//----------------------------------------------------//

//---- Add load Event
//---- param: function
//---- summary: Adds function to onload event.
//---- Allows to load controls after page is loaded
function addLoadEvent(func) {
    if (func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = function () {
                func();
                g_onLoadCalled = true;
            };
        }
        else {
            window.onload = function () {
                oldonload();
                func();
                g_onLoadCalled = true;
            };
        }
    }

    if (typeof (CallBirdsEyeAvailabilityCallback) != 'undefined') {
        addBirdsEyeUpdateEvent();
    }
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

    document.body.appendChild(form);
    form.submit();
}
function scrollTopTo(id){
	try{
		$(window).scrollTop($('#'+id).position().top-75);
	}
	catch(ex){}
}

function onEnterSubmit(id, e,submitFn,otherFn){
	var isEnter = window.event == null ? 
            e.keyCode == 13 : 
            window.event.keyCode == 13;
	if(isEnter){
	  eval(submitFn);
	  return;
	}
	else{
		if(otherFn!=undefined)eval(otherFn);
	}
}

function fadeIn(element, opacity) {
	var reduceOpacityBy = 5;
	var rate = 30;	// 15 fps


	if (opacity < 100) {
		opacity += reduceOpacityBy;
		if (opacity > 100) {
			opacity = 100;
		}

		if (element.filters) {
			try {
				element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
			}
		} else {
			element.style.opacity = opacity / 100;
		}
	}

	if (opacity < 100) {
		setTimeout(function () {
			fadeIn(element, opacity);
		}, rate);
	}
}

function resizeImage(newWidth,newHeight,fixWidth,fixHeight){ 
	var ratio=fixWidth/newWidth;	
	if(ratio>fixHeight/newHeight)ratio=fixHeight/newHeight;	
	this.width=newWidth*ratio;
	this.height=newHeight*ratio;
}

function SetCookie(psName, psValue, piExpires, psPath, psDomain, psSecure) {
    var ldToday = new Date();
    ldToday.setTime(ldToday.getTime());

    if (piExpires) { piExpires = piExpires * 1000 * 60 * 60 * 24; }
    var ldExpiresDate = new Date(ldToday.getTime() + (piExpires));

    document.cookie = psName + "=" + escape(psValue) +
				    ((piExpires) ? ";expires=" + ldExpiresDate.toGMTString() : "") +
				    ((psPath) ? ";path=" + psPath : "") +
				    ((psDomain) ? ";domain=" + psDomain : "") +
				    ((psSecure) ? ";secure" : "");
}

function GetCookie(psName) {
    var liStart = document.cookie.indexOf(psName + "=");
    var liLen = liStart + psName.length + 1;
    if ((!liStart) && (psName != document.cookie.substring(0, psName.length))) { return null; }
    if (liStart == -1) { return null; }
    var liEnd = document.cookie.indexOf(";", liLen);
    if (liEnd == -1) {
        liEnd = document.cookie.length;
    }
    return unescape(document.cookie.substring(liLen, liEnd));
}

function DeleteCookie(psName, psPath, psDomain) {
    if (fnGetCookie(psName)) {
        document.cookie = psName + "=" +
	    ((psPath) ? ";path=" + psPath : "") +
	    ((psDomain) ? ";domain=" + psDomain : "") +
	    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}
function nFormatter(num) {
    if (num >= 1000000000) {
        return (num / 1000000000).toFixed(1) + 'G';
    }
    if (num >= 1000000) {
        return (num / 1000000).toFixed(1) + 'M';
    }
    if (num >= 1000) {
        return (num / 1000).toFixed(1) + 'K';
    }
    return num;
}

function t(o) {
	  var out = '';
	  for (var p in o) {
	    out += p + ': ' + o[p] + '\n';
	  }
	  alert(out);
	}
function randomString(num)
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < num; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}